﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace VigenereCipher.Models
{
    /// <summary>
    /// 实现加密
    /// </summary>
    internal class MyEncoder
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string Plain { get; set; }

        /// <summary>
        /// 密钥
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 密文
        /// </summary>
        public string Cipher { get; set; }
        //加密过程
        public void encode()
        {
            //将明文的字符依次加密
            for (int i = 0,j = 0; i < Plain.Length; i++)
            {
                //按照规则加密字符
                Cipher += Convert.ToChar((Plain[i] + CharToNo(Key[j])));
                j ++;
                //按照密钥的长度循环
                if (j > Key.Length - 1)
                    j = 0;
            }
            //Cipher = Cipher.ToString();
        }
        /// <summary>
        /// 将密钥字符转换成它在字母表中的顺序（0-25）
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int CharToNo(char ch)
        {
            return ch - 'a';
        }
    }
}
